<?php
namespace App\Http\Controllers\ApiXcx;

use Illuminate\Http\Request;

class WxCaseoutController extends BaseController
{
    //微信支付

    //必须的参数
    protected $app_id = 'wx081bb28635ee4455'; //小程序ID
    protected $mch_id = '1683618974'; //商户号
    protected $key = 'karcvn13a3mj1yfysww0dzcnbic4ec8r'; //秘钥
    protected $notify_url = ''; //通知接口

    public function __construct()
    {
        $this->notify_url = env('DOMAIN') . '/api/xcx/caseout/notice';
    }

    //提现接口
    public function wxout(Request $request)
    {
        $nonce = toOrderno();
        $orderno = toOrderno();
        $amount = 1;
        $openid = 'oyzUn7d22Zt_6fXzc66en6e8HIKA';
        $type_name = 'tixian';
        $check_name = 'NO_CHECK';
        //构建签名
        $signs = [
            'mch_appid' => $this->app_id,
            'mchid' => $this->mch_id,
            'nonce_str' => $nonce,
            //'notify_url' => $this->notify_url,
            'partner_trade_no' => $orderno,
            'amount' => $amount,
            'desc' => $type_name,
            'openid' => $openid,
            'check_name' => $check_name,
        ];
        ksort($signs);
        $string = '';
        foreach ($signs as $k => $v) {
            if ($v) {
                $string .= $k . '=' . $v . '&';
            }
        }
        $string .= 'key=' . $this->key;
        var_dump($string); echo '<br>string<br>';
        $signStr = strtoupper(md5($string));
        //构建请求参数
        $param = '';
        $param .= '<xml>';
        foreach ($signs as $k => $v) {
            $param .= '<'.$k.'>' . $v . '</'.$k.'>';
        }
//        $param .= '<mch_appid>' . $this->app_id . '</mch_appid>';
//        $param .= '<mchid>' . $this->mch_id . '</mchid>';
//        $param .= '<desc>' . $type_name . '</desc>';
//        $param .= '<nonce_str>' . $nonce . '</nonce_str>';
//        $param .= '<notify_url>' . $this->notify_url . '</notify_url>';
//        $param .= '<partner_trade_no>' . $orderno . '</partner_trade_no>';
//        $param .= '<openid>' . $openid . '</openid>';
//        $param .= '<check_name>' . $check_name . '</check_name>';
//        $param .= '<amount>' . $amount . '</amount>';
        $param .= '<sign>' . $signStr . '</sign>';
        $param .= '</xml>';
        var_dump($param); echo '<br>param<br>';
        $httpUrl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $res = $this->toCurl($httpUrl, $param, 1);
        ddd($res);
    }

    //通知
    public function notice(Request $request)
    {
        file_put_contents('wxcaseout.log',
            toDate().'：'.json_encode($request->all(), JSON_UNESCAPED_UNICODE).PHP_EOL,
            FILE_APPEND);
        return $this->toJson();
    }

    /**
     * 构建CURL请求，单线程
     * @param string $httpUrl 请求地址
     * @param string $string 参数拼接的字符串
     * @return $json
     */
    public function toCurl($httpUrl = '', $string = '', $is_pem = 0)
    {
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$httpUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($curl, CURLOPT_ENCODING, '');
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json', 'Content-Length: ' . strlen($string)
        ));
        if ($is_pem) {
            curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($curl, CURLOPT_SSLCERT, '/www/tiepi/public/apiclient_cert.pem');
            curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($curl, CURLOPT_SSLKEY, '/www/tiepi/public/apiclient_key.pem');
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        ddd($output);
    }
}